package com.data;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.cards.Car;


public class carTable extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int    DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME    = "db_car";

    // Contacts table name
    private static final String TABLE_CAR        = "tbl_car";

    // Contacts Table Columns names
    private static final String ID               = "id";
    private static final String SPEED            = "speed";
    private static final String ACC              = "acc";
    private static final String MOTOR_VOL        = "motor_val";
    private static final String CYLINDER         = "cylinder";
    private static final String WEIGHT           = "weight";


    /**
     * Constructor
     * 
     * @param context to use to open or create the database
     * */
    public carTable(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    // Creating Tables

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CAR_TABLE = TABLE_CAR + "("
                + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + SPEED + " INTEGER,"
                + ACC + " INTEGER," + MOTOR_VOL + " INTEGER, " + CYLINDER + " INTEGER," + WEIGHT + " INTEGER" + ");";
        db.execSQL("CREATE TABLE IF NOT EXISTS " + CREATE_CAR_TABLE);
    }


    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CAR);

        // Create tables again
        onCreate(db);
    }


    public long addCar(Car car) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(SPEED, car.getSpeed());
        values.put(ACC, car.getAcc());
        values.put(MOTOR_VOL, car.getMotorVol());
        values.put(CYLINDER, car.getCylinder());
        values.put(WEIGHT, car.getWeight());

        // Inserting Row
        long result = db.insert(TABLE_CAR, null, values);
        db.close(); // Closing database connection
        return result;
    }


    Car getCar(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_CAR, new String[]{ ID, SPEED,
                ACC, MOTOR_VOL, CYLINDER, WEIGHT }, ID + "=?",
                new String[]{ String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Car car = new Car(cursor.getInt(0), cursor.getInt(1),
                cursor.getInt(2), cursor.getInt(3),
                cursor.getInt(4), cursor.getInt(5));

        db.close();
        cursor.close();

        // return car
        return car;
    }


    public List<Car> getAllCars() {
        List<Car> carList = new ArrayList<Car>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CAR;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list

        if (cursor.moveToFirst()) {
            do {
                Car car = new Car(cursor.getInt(0), cursor.getInt(1),
                        cursor.getInt(2), cursor.getInt(3),
                        cursor.getInt(4), cursor.getInt(5));

                // Adding car to list
                carList.add(car);
            }
            while (cursor.moveToNext());
        }

        db.close();
        cursor.close();

        // return carList
        return carList;
    }


    public int updateCar(Car car) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(SPEED, car.getSpeed());
        values.put(ACC, car.getAcc());
        values.put(MOTOR_VOL, car.getMotorVol());
        values.put(CYLINDER, car.getCylinder());
        values.put(WEIGHT, car.getWeight());

        // updating row
        int result = db.update(TABLE_CAR, values, ID + " = ?",
                new String[]{ String.valueOf(car.getId()) });
        db.close();
        return result;
    }


    public int deleteContact(Car car) {
        SQLiteDatabase db = this.getWritableDatabase();
        int result = db.delete(TABLE_CAR, ID + " = ?",
                new String[]{ String.valueOf(car.getId()) });
        db.close();
        return result;
    }


    public int getNumberOfCars() {
        String countQuery = "SELECT  * FROM " + TABLE_CAR;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);

        int result = cursor.getCount();

        db.close();
        cursor.close();

        return result;
    }

    /*public List<Poem> searchTag(String tag) {

        List<Poem> poemList = new ArrayList<Poem>();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.query(TABLE_POEM, new String[]{ ID, NAME,
                POET, STAR, TAG, BOOK, TEXT }, TAG + " LIKE ?",
                new String[]{ "%" + tag + "%" }, null, null, null, null);

        // looping through all rows and adding to list

        if (cursor.moveToFirst()) {
            do {
                Poem poem = new Poem();
                poem.setId(Integer.parseInt(cursor.getString(0)));
                ...

                // Adding poem to list
                poemList.add(poem);
            }
            while (cursor.moveToNext());
        }

        db.close();
        cursor.close();

        // return pomlist
        return poemList;
    }*/

    /*public void setStar(int poemId, int star)
    {
        Poem p;
        p = getPoem(poemId);
        p.setStar(star);
        updatePoem(p);
    }*/
}
